/*
 Copyright (C) 2010 Kristian Duske

 This file is part of TrenchBroom.

 TrenchBroom is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 TrenchBroom is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "Result.h"
#include "mdl/BrushFaceHandle.h"

#include "vm/plane.h"

#include <vector>

namespace tb::mdl
{
class BrushNode;

/**
 * A brush face reference creates a persistent reference to a face that has a specific
 * boundary plane. It can be resolved later, and if the brush then has a face with the
 * same boundary plane, that face will be returned.
 *
 * Depending on how the brush was modified in time since the reference has been created,
 * the resolved face might not be the same as the face which the reference was created
 * with.
 */
class BrushFaceReference
{
private:
  BrushNode* m_node;
  vm::plane3d m_facePlane;

public:
  /**
   * Creates a new reference to the given face.
   *
   * @param node the containing brush node, must not be null
   * @param face the face to reference
   */
  BrushFaceReference(BrushNode* node, const BrushFace& face);

  /**
   * Resolves the referenced brush face or an error if this reference cannot be resolved.
   */
  Result<BrushFaceHandle> resolve() const;
};

/**
 * Returns a vector of brush face references for faces represented by the given handles.
 */
std::vector<BrushFaceReference> createRefs(const std::vector<BrushFaceHandle>& handles);

/**
 * Returns a vector brush face handles representing the faces to which the given face
 * references are resolved or an error if any reference cannot be resolved.
 */
Result<std::vector<BrushFaceHandle>> resolveAllRefs(
  const std::vector<BrushFaceReference>& faceRefs);

} // namespace tb::mdl
